<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Api;

use App\Constants\ErrorCode;
use App\Constants\SuccessCode;
use App\Controller\FuelOrders;
use App\Exception\BusinessException;
use App\Model\Account;
use App\Model\ActivityCommission;
use App\Model\ActivityOrder;
use App\Model\ElectricOrder;
use App\Model\FuelOrder;
use App\Model\Invitation;
use App\Model\LoginRecord;
use App\Model\OrderRecord;
use App\Model\TeamCommission;
use App\Model\TelephoneOrder;
use App\Model\TopExtractOrder;
use App\Model\TransactOrder;
use App\Model\User;
use App\Model\UserFullReward;
use App\Model\UserInviteGrade;
use App\Request\Api\Auth\UserBalanceRecordSelectRequest;
use App\Request\Api\Auth\UserLoginRequest;
use App\Request\Api\Auth\UserMobileUpdateRequest;
use App\Request\Api\Auth\UserPwdUpdateRequest;
use App\Request\Api\Auth\UserRegisterRequest;
use App\Request\Api\UserUpdateRequest;
use App\Resource\Api\InvitationResource;
use App\Resource\Api\LevelResource;
use App\Resource\Api\LevelRewardResource;
use App\Resource\Api\UserBalanceRecordResource;
use App\Resource\Api\UserBalanceResource;
use App\Resource\Api\UserLoginRecordResource;
use App\Resource\BaseResource;
use App\Service\AuthServer;
use App\Traits\PwdTool;
use App\Controller\AbstractController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use function Swoole\Coroutine\Http\get;

class Auth extends AbstractController
{
    use PwdTool;

    /**
     * @var AuthServer
     */
    #[Inject]
    protected $authServer;

    public function register(UserRegisterRequest $request)
    {
        return $this->authServer->register($request);
    }

    public function login(UserLoginRequest $request)
    {
        return $this->authServer->login($request);
    }

    public function getUserShare(RequestInterface $request)
    {
        $user = User::find(get_app_user()->id);
        if ($user->is_share == 1) {

            return new BaseResource(SuccessCode::SUCCESS, [

                'qrcode_path' => createdQrcode($user->invite_code, $user->nick_name),
            ]);
        }

        throw new BusinessException(ErrorCode::AUTH_USER_SHARE_ERROR);
    }

    public function getUser(RequestInterface $request)
    {
        return $this->authServer->getUserInfo($request);
    }

    public function refreshToken(RequestInterface $request)
    {
        return $this->authServer->refreshToken($request);
    }

    public function updatePwd(UserPwdUpdateRequest $request)
    {
        return $this->authServer->updatePwd($request);
    }

    public function setUserInfo(UserUpdateRequest $request)
    {
        return $this->authServer->setUserInfo($request);
    }

    public function getUserBalance(RequestInterface $request)
    {
        $accoun = Account::where('uid', get_app_user()->id)->first();

        return new \App\Resource\UserBalanceResource($accoun);
    }

    public function getMyInvitations(RequestInterface $request)
    {
        $list = Invitation::where('invitation_user_id', get_app_user()->id)->orderByDesc('id')->paginate();

        return InvitationResource::collection($list)->additional(['code' => 0, 'msg' => '成功']);
    }

    public function getBalanceUnusableOrders(RequestInterface $request)
    {
        $orders = OrderRecord::where('capital_type', OrderRecord::CAPITAL_TYPE_BALANCE_UNUSABLE)
            ->where('user_id', get_app_user()->id)
            ->orderByDesc('id')
            ->paginate();

        return \App\Resource\Backend\BaseResource::collection($orders)->additional(['code' => 0, 'msg' => '成功']);
    }

    public function loginOut(RequestInterface $request)
    {
        $user = User::find(get_app_user()->id);
        $user->register_id = '';
        $user->save();

        return new BaseResource();
    }

    public function getUserAssets()
    {
        $all_amount = Account::where('uid', get_app_user()->id)->first();

        /*
        $topUpAmount = TopExtractOrder::where('type', TopExtractOrder::PAY_TYPE_TOP_UP)
            ->where('top_state', TopExtractOrder::PAY_TOP_UP_SUCCESS)
            ->where('uid', get_app_user()->id)
            ->sum('real_amount');

        $transactAmount = TransactOrder::where('pay_uid', get_app_user()->id)
            ->where('state', TransactOrder::TRANSACT_ORDER_SUCCESS)
            ->sum('receipt_amount');

        $commission = TeamCommission::where('commission_uid', get_app_user()->id)
            ->sum('commission_price');

        $agentCommission = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_AGENT)
            ->sum('amount');

        $activityDirectAmount = ActivityCommission::where('commission_uid', get_app_user()->id)
            ->where('order_type', 1)
            ->sum('commission_real_amount');

        $activitySeeAmount = ActivityCommission::where('commission_uid', get_app_user()->id)
            ->where('order_type', 2)
            ->sum('commission_real_amount');
        */

        $topUpAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_TOP_UP)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_BALANCE_USABLE)
            ->sum('amount');

        $transactAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_TRANSCTION)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_BALANCE_USABLE)
            ->sum('amount');

        $commission = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type',OrderRecord::ORDER_TYPE_SEE)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_BALANCE_USABLE)
            ->sum('amount');

        $agentCommission = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type',OrderRecord::ORDER_TYPE_AGENT)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_BALANCE_USABLE)
            ->sum('amount');

        $activityDirectAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type',OrderRecord::ORDER_TYPE_ACTIVITY_DIRECT)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_BALANCE_USABLE)
            ->sum('amount');
        $activitySeeAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type',OrderRecord::ORDER_TYPE_ACTIVITY_SEE)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_BALANCE_USABLE)
            ->sum('amount');

        /*
        $extractAmount = TopExtractOrder::where('type', TopExtractOrder::PAY_TYPE_EXTRACT)
            ->where('extract_state', TopExtractOrder::PAY_EXTRACT_SUCCESS)
            ->where('uid', get_app_user()->id)
            ->sum('real_amount');

        $phoneAmount = TelephoneOrder::where('uid', get_app_user()->id)
            ->where('state', ElectricOrder::PAY_SUCCESS)
            ->sum('real_amount');

        $activityAmount = ActivityOrder::where('uid', get_app_user()->id)
            ->where('state', ElectricOrder::PAY_SUCCESS)
            ->withTrashed()
            ->sum('real_amount');

        $fuelAmount = FuelOrder::where('uid', get_app_user()->id)
            ->where('state', ElectricOrder::PAY_SUCCESS)
            ->sum('real_amount');

        $electricAmount = ElectricOrder::where('uid', get_app_user()->id)
            ->where('state', ElectricOrder::PAY_SUCCESS)
            ->sum('real_amount');

        $transactSellAmount = TransactOrder::where('sell_uid', get_app_user()->id)
            ->where('state', TransactOrder::TRANSACT_ORDER_SUCCESS)
            ->sum('receipt_amount');
        */

        $extractAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_EXTRACT)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_DEDUCTION_BALANCE)
            ->sum('amount');

        $phoneAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_TELEPHONE)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_DEDUCTION_BALANCE)
            ->sum('amount');

        $activityAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_ACTIVITY)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_DEDUCTION_BALANCE)
            ->sum('amount');

        $fuelAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_FUEL)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_DEDUCTION_BALANCE)
            ->sum('amount');

        $electricAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_ELECTIC)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_DEDUCTION_BALANCE)
            ->sum('amount');

        $transactSellAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_TRANSCTION)
            ->where('capital_type', OrderRecord::CAPITAL_TYPE_DEDUCTION_BALANCE)
            ->sum('amount');

        $otherDisburseAmount = OrderRecord::where('uid', get_app_user()->id)
            ->where('order_type', OrderRecord::ORDER_TYPE_AGENT_MONEY)
            ->sum('amount');


        $unusableAmount = $all_amount->electric_unusable + $all_amount->fuel_unusable + $all_amount->telephone_unusable + $all_amount->extract_unusable + $all_amount->transact_unusable + $all_amount->bonus_unusable;

        $refundAmount1 = OrderRecord::where('uid', get_app_user()->id)
            ->whereIn('capital_type', [OrderRecord::CAPITAL_TYPE_PRINCIPAL, OrderRecord::CAPITAL_TYPE_TOP_UP_CANCEL])
            ->sum('amount');

        $refundAmount2 = OrderRecord::where('uid', get_app_user()->id)
            ->sum('refund_amount');

        return new BaseResource(SuccessCode::SUCCESS, [

            'other_all_amount' => formatNumbers( $unusableAmount + $refundAmount1 + $refundAmount2),
            'other_unusable_amount' => formatNumbers($unusableAmount),
            'other_refund_amount' => formatNumbers($refundAmount1 + $refundAmount2),

            'disburse_all' => formatNumbers( $extractAmount + $phoneAmount + $fuelAmount + $electricAmount + $transactSellAmount + $otherDisburseAmount),

            'disburse_extract_amount' => formatNumbers($extractAmount),
            'disburse_phone_amount' => formatNumbers($phoneAmount + $activityAmount),
            'disburse_fuel_amount' => formatNumbers($fuelAmount),
            'disburse_electric_amount' => formatNumbers($electricAmount),
            'disburse_transact_sell_amount' => formatNumbers($transactSellAmount),
            'disburse_other_disbures_amount' => formatNumbers($otherDisburseAmount),

            'all_amount' => formatNumbers($all_amount->balance_usable),

            'income_top_up' => formatNumbers($topUpAmount),
            'income_transact_amount' => formatNumbers($transactAmount),
            'income_commission' => formatNumbers($commission),
            'income_agent_commission' => formatNumbers($agentCommission),
            'income_activity_direct_amount' => formatNumbers($activityDirectAmount),
            'income_activity_see_amount' => formatNumbers($activitySeeAmount),
            'income_all_amount' => formatNumbers( $topUpAmount + $transactAmount + $commission + $agentCommission + $activityDirectAmount + $activitySeeAmount)
        ]);
    }

}
